﻿using AiQiuQuan.Sport.Model.Enums;
using Microsoft.EntityFrameworkCore;

namespace AiQiuQuan.Sport.WebApi.HangfireServices
{
    /// <summary>
    /// 课程订单自动取消
    /// </summary>
    public partial class AutoCancelOrderHangfireService
    {
        /// <summary>
        /// 课程订单自动取消
        /// </summary>
        public async Task CourseExecuteAsync()
        {
            var orderIdList = await _courseOrderRepository.AsNoTracking()
                .Where(m => m.State == CourseOrderStateEnum.NotPay && m.CreateTime < DateTimeOffset.Now.Date)
                .Select(m => m.ID)
                .ToListAsync();
            if (orderIdList.Any())
            {
                var builder = await _courseOrderRepository.UpdatePartialAsync();
                await builder.Set(m => m.State, CourseOrderStateEnum.Cancel)
                .Where(m => orderIdList.Contains(m.ID))
                .ExecuteAsync();
            }
        }
    }
}
